import { ref } from 'vue';
import mcApi from 'mc-c2b2m-operator-api';
import { FormSchema } from '@/components/Form';
import { uiComponentList } from '@/enums/spuEnum';
import { SpuSpecModel, PricingCalculatorModel } from 'mc-c2b2m-operator-api/types/spu';

const { SpuModule } = mcApi;
const { QuotationApi } = SpuModule;

const pricingCalculatorList = ref<any>([]);
// 获取价格计算器
const result = await QuotationApi.getPricingCalculatorPage({ display: true });
pricingCalculatorList.value =
  result?.items?.map?.((item: PricingCalculatorModel) => {
    const { id, cnName } = item;
    return {
      key: id,
      value: id,
      label: cnName,
    };
  }) || [];

// spu规格项表单配置
export const specItemFormSchema: FormSchema[] = [
  { required: true, label: '展示名称', field: 'displayName', component: 'Input' },
  { required: true, label: '最大数量', field: 'maxQuantity', component: 'InputNumber' },
  { required: true, label: '最小数量', field: 'minQuantity', component: 'InputNumber' },
  {
    required: true,
    label: 'UI组件',
    field: 'uiComponent',
    component: 'Select',
    componentProps: {
      options: uiComponentList,
    },
  },
  {
    label: '价格计算器',
    field: 'pricingCalculatorId',
    component: 'Select',
    componentProps: {
      options: pricingCalculatorList.value,
    },
  },
];
specItemFormSchema.forEach((item: FormSchema) => {
  item.colProps = { span: 6 };
});

// spu规格组表单配置
export const specGroupFormSchema: FormSchema[] = [
  { required: true, label: '组展示名称', field: 'displayName', component: 'Input' },
  {
    required: true,
    label: 'UI组件',
    field: 'uiComponent',
    component: 'Select',
    componentProps: {
      disabled: true,
      options: uiComponentList,
    },
  },
  { required: true, label: '最大数量', field: 'maxQuantity', component: 'InputNumber' },
  { required: true, label: '最小数量', field: 'minQuantity', component: 'InputNumber' },
  {
    label: '价格计算器',
    field: 'pricingCalculatorId',
    component: 'Select',
    componentProps: {
      options: pricingCalculatorList.value,
    },
  },
];
specGroupFormSchema.forEach((item: FormSchema) => {
  item.colProps = { span: 8 };
});

// 默认的规格模型
export const defaultSpuSpec: SpuSpecModel = {
  type: null,
  id: '',
  spuId: '',
  parentId: '',
  valueMethod: '',
  displayName: '',
  uiComponent: '',
  bizMetaAttrId: '',
  maxQuantity: null,
  minQuantity: null,
  optValueList: [],
  childSpuSpecList: [],
};
